package com.yc.supermarket.web.controller;

import com.yc.supermarket.entity.Sales;
import com.yc.supermarket.entity.Transaction;
import com.yc.supermarket.service.SalesService;
import com.yc.supermarket.service.impl.SalesServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/order")
public class OrderController extends BaseController{
    private SalesService service=new SalesServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String op=req.getParameter("op");
        if ("finds".equals(op)) {
            dofinds(req, resp);
        }else if("findAll".equals(op)){
            dofindAll(req,resp);
        }else if("findone".equals(op)){
            dofindOne(req,resp);
        }else{
            resp.getWriter().print("weigenu such method!!");
        }

    }
    protected void dofinds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Sales ordertail=parseRequest(req, Sales.class);
        System.out.println("the:"+ordertail);
        Map<String,Object> map=service.finds(ordertail);
        printToJson(resp,map);
    }
    protected void dofindAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Transaction order=parseRequest(req, Transaction.class);
        System.out.println("the:"+order);
        Map<String,Object> map=service.findAll(order);
        System.out.println("thwe:"+map);
        printToJson(resp,map);
    }
    protected void dofindOne(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Transaction order=parseRequest(req,Transaction.class);
        Map<String,Object> map=service.findOne(order);
        System.out.println("thefgr:"+map);
        printToJson(resp,map);
    }

}
